<?php
class Admin_AboutusController extends Zend_Controller_Action
{
    protected $_em;
    protected $_repo;
    protected $_flash;
	
    public function init()
    {
    	$this->_em = Zend_Registry::get('doctrine');
    	$this->_flash = $this->_helper->getHelper('FlashMessenger');
    	
    	
    }
    
    ///////////////// index //////////////////////
	
    public function indexAction()
	{
	   
	}
	///////////////// News Letter //////////////////////
	
	public function newsletterAction()
	{
		$this->view->manager = 'MANAGEMENT NEWSLETTER LIST';
		 
		$id = $this->_getParam('id', 0);
		 
		$this->_repo = $this->_em->getRepository('Entities\HaeNewsletter');
		if ( $id != 0 )
		{
			$this->_repo->deleteById($id);
		}
		$this->view->object = $this->_repo->findAll();
	}
	///////////////// career //////////////////////
	
	public function careerAction()
	{
	    $this->view->manager = 'MANAGEMENT CAREER LIST';
	    
	    $id = $this->_getParam('id', 0);
	    
	    $this->_repo = $this->_em->getRepository('Entities\HaeCareer');
	    if ( $id != 0 )
	    {
	    	$this->_repo->deleteById($id);
	    }
	    $this->view->object = $this->_repo->findAll();
	}
	
	public function detailcareerAction()
	{
	    $id = $this->_getParam('id', 0);
	    
	    $form = new Admin_Form_Career();
	    $this->view->form = $form;
	    $this->_repo = $this->_em->getRepository('Entities\HaeCareer');
	    if($id == 0 )
	    {
	    	$this->view->manager = 'ADD NEW CAREER';
	    	if($this->getRequest()->isPost())
	    	{
	    		$this->_repo->saveAll($id,'Career',$this->getRequest()->getPost());
	    		$this->_helper->flashMessenger('Add successfully');
	    	}
	    }
	    else
	    {
	        $this->_repo = $this->_em->getRepository('Entities\HaeCareer');
	    	if($this->getRequest()->isPost())
	    	{
	    		$this->_repo->saveAll($id,'Career',$this->getRequest()->getPost());
	    		$this->_helper->flashMessenger('Edit successfully');
	    	}
	    	$data = $this->_repo->findOneById($id);
	    	$this->_repo = $this->_em->getRepository('Entities\HaeSeo');
	    	$dataSeo = $this->_repo->findOneById($data->getseoId());
	    	
	    	$this->view->manager = 'EDITOR  CAREER :'.$data->getTitleEn();
	    	$form->populate($data->toArray());
	    	$form->populate($dataSeo->toArray());
	    }
	}
	
	///////////////// partner //////////////////////	
	
	public function partnerAction()
	{
	    
	}
	public function typeimage($name)
	{
		$names = explode('.', $name);
		return $names[count($names)-1];
	}
	
	public function addimage($id){
		$this->_repo = $this->_em->getRepository('Entities\HaePartner');
		$partner = $this->_repo->findOneById($id);
		 
		$typeimage=  $this->typeimage($_FILES['image']['name']);
		$nameimage = $id.'_'.'partner.'.$typeimage;
		 
		$partner->setImage($nameimage);
		$this->_em->persist($partner);
		$this->_em->flush();
		 
		$url_dir = PUBLIC_PATH."/files/images/partner/".$nameimage;
		$image_saved = move_uploaded_file($_FILES['image']['tmp_name'],$url_dir);
	}
	
	public function deletepartnerAction()
	{
		$id = $this->_getParam('id', 0);
		$db_partner = new  Admin_Model_DbTable_partner();
		$db_partner->deletepartner($id);
		$this->_redirect('/admin/aboutus/partner');
	}
	
}
?>